package com.example.myapplication2.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TestActivity extends Activity implements View.OnClickListener {
    private static Integer counter = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_test);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(this);
        Button dialogBtn = (Button) findViewById(R.id.button_dialog);
        dialogBtn.setOnClickListener(this);
        Button multiBtn = (Button) findViewById(R.id.button_multi);
        multiBtn.setOnClickListener(this);
        Button chatBtn = (Button) findViewById(R.id.btn_chat);
        chatBtn.setOnClickListener(this);
        Button networkBtn = (Button) findViewById(R.id.state_btn);
        networkBtn.setOnClickListener(this);
        Button broadBtn = (Button) findViewById(R.id.btn_send_broad);
        broadBtn.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case RESULT_OK:
                if (counter == data.getIntExtra("counter", 1)) {
                    counter++;
                } else {
                    Log.e("点击计数", "计数失败");
                }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add:
                Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show();
                break;
            case R.id.menu_remove:
                Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.button_1:
                intent = new Intent(TestActivity.this, SecondActivity.class);
                intent.putExtra("counter", counter);
                startActivityForResult(intent, 1);
                break;
            case R.id.button_dialog:
                intent = new Intent(TestActivity.this, DialogActivity.class);
                startActivity(intent);
                break;
            case R.id.button_multi:
                intent = new Intent(TestActivity.this, MultipleActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_chat:
                intent = new Intent(TestActivity.this, ChatActivity.class);
                startActivity(intent);
                break;
            case R.id.state_btn:
                intent = new Intent(TestActivity.this, BroadcastActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_send_broad:
                intent = new Intent("com.example.myapplication2.app.BROAD_SEND");
                EditText text = (EditText) findViewById(R.id.broad_edit_text);
                intent.putExtra("data", text.getText().toString());
                sendBroadcast(intent);
                text.setText("");
                break;
        }
    }
}
